/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseserver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ntfsbk
 */
public class ManageFileThread extends Thread{
    JTable table;
    boolean runining=true;
    public ManageFileThread(){
        
    }
    public ManageFileThread(JTable table){
        this.table=table;
    }
    public boolean isRunning(){
        return  runining;
    }
    public void setRunning(boolean running){
        this.runining=running;
    }
    
    @Override
    public void run(){
        
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;"
                    + "databaseName=sharing;userName=sa;password=123456";
            while (true) {                
                if(isRunning()){
                    Connection conn = DriverManager.getConnection(connectionUrl);
                    Statement statement = conn.createStatement();
                    String sqlQuery="select * from files";
                    ResultSet rs=statement.executeQuery(sqlQuery);
                    int i=0;
                    DefaultTableModel model=(DefaultTableModel) table.getModel();
                    model.setRowCount(0);
                    while(rs.next()){
                        i++;
                        int fileId=rs.getInt("fileId");
                        String fileName=rs.getString("fileName");
                        float fileSize=rs.getFloat("fileSize");
                        model.addRow(new Object[]{
                            i+1,fileId,fileName,fileSize
                        });
                    }
                }
                else
                    return;
                Thread.currentThread().sleep(3000);
            }
        } catch (Exception e) {
            System.out.println("run in ManageUserThread");
            e.printStackTrace();
        }
    }
}
